class BookingController {

    def bookingHotelFlow = {
        enter {
            action {
                Hotel hotel = Hotel.get(params.id)
                flow.hotel = hotel
            }

            on('success').to 'viewHotel'
        }

        viewHotel {
            on('bookHotel') {
                Booking booking = new Booking(user: session.LOGIN_USER, hotel: flow.hotel)
                booking.checkinDate = new Date()
                booking.checkoutDate = new Date() + 1
                flow.booking = booking
            }.to 'bookHotel'

            on('backToSearch').to 'backToSearch'
        }

        bookHotel {
            on('proceed') {
                flow.booking.properties = params
                flow.booking.validate() ? success() : error()
            }.to 'proceed'
            on('backToSearch').to 'backToSearch'
        }

        proceed {
            on('confirm') {
                flow.booking.save()
            }.to 'confirm'
            on('backToSearch').to 'backToSearch'
        }

        confirm {
            redirect(controller: 'hotel', action: 'index')
        }

        backToSearch {
            redirect(controller: 'hotel', action: 'index')
        }
    }

    def cancel = {
        Booking booking = Booking.get(params.id)
        if (booking) {
            booking.delete()
            flash.message = "Booking cancelled for confirmation number ${booking.id}"
            redirect(controller: 'hotel', action: 'index')
        }
    }
}
